var express = require('express');
var financeModel = require("../database/financeModel");
var router = express.Router();

/* GET users listing. */
router.get('/list', async function (req, res, next) {
  try {
    let { date, cost, way,pageSize,current } = req.query;//获取传来的name,address
    let obj = {};
    if (date) {
      obj.date = new RegExp(date);//正则表达式实现模糊查询
    }
    if (cost) {
      // 处理age作为数字的模糊查询
      let contactQuery = parseInt(cost); // 将age转换为整数

      // 定义contact的范围查询
      obj.cost = {
        $gte: contactQuery - 1000, // 假设模糊匹配为大于等于(age-1000)
        $lte: contactQuery + 1000  // 假设模糊匹配为小于等于(age+)
      };
    }
    if (way) {
      obj.way = new RegExp(way);//正则表达式实现模糊查询
    }
    //查询对应页的数据
    let skip =(current-1)*pageSize//从哪一条数据开始
    // let result = await studentModel.find(obj);//没有分页
    let result=await financeModel.find(obj).skip(skip).limit(pageSize);//分页的
    let total=await financeModel.find(obj).countDocuments();//总条数
    console.log("total",total)
    res.send({ code: 0, data: result, message: "成功",count:total });
  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

router.get('/del', async function (req, res, next) {
  try {
    let { _id } = req.query;
    let result = await financeModel.deleteOne({ "_id": _id });
    if (result.deletedCount > 0) {
      res.send({ code: 0, message: "删除成功" })
    }
    else {
      res.send({ code: 1, message: "删除失败" })
    }
  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

//添加
router.post('/add', async function (req, res, next) {
  try {
    let { date, cost, way } = req.body

    let result = await financeModel.insertMany([{ date, cost, way }])//返回Promise对象
    console.log(req.body)
    if (result.length > 0) {
      res.send({ code: 0, data: result, message: "添加成功" });
    }
    else {
      res.send({ code: 1, message: "添加失败" });
    }
    // res.send({ code: 0, data: result, message: "成功" });
  } catch (error) {

    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

//根据id查找对应的数据
router.get('/getByid', async function (req, res, next) {
  try {
    let { id } = req.query;
    let result = await financeModel.findOne({ "_id": id })//查找的键名叫"_id" 查到的id给上面的id
    console.log(req.query);
    console.log(result);
    res.send({ code: 0, message: "成功", data: result });

  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

//根据id更新数据
router.post('/update', async function (req, res, next) {
  try {
    let { id, date, cost, way } = req.body

    let result = await financeModel.updateOne({ "_id": id }, { $set: { date, cost, way } })
    console.log(req.query)
    console.log(result);
    if (result.modifiedCount > 0) {
      res.send({ code: 0, message: "修改成功" })
    }
    else {
      res.send({ code: 0, message: "修改失败" });
    }


  } catch (error) {

    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

module.exports = router;